package com.sali.design_pattern.c_factory_method_pattern.lesson_03.service;

import com.sali.design_pattern.c_factory_method_pattern.lesson_03.pojo.User;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.stereotype.Service;

/**
 * @description: 原始逻辑
 * @author: zhang hang
 * @date: 2022/11/5 09:39.
 * @version: 1.0
 */
@Service
@Slf4j
public class UserServiceImpl implements com.sali.design_pattern.c_factory_method_pattern.lesson_03.service.IUserService {

    @Override
    public User getUser(Long id) {
        User user = User.builder().id(1L).vip(3).username("张萨丽").password("123456").build();
        return user;
    }

    @Override
    public String buyCourse(Long userId, Long courseId) {
        User user = this.getUser(userId);
        if (user != null) {
            Integer vip = user.getVip();
            Double money = 100d;
            Integer corn = 0;

            // 此处破坏了设计模式中的单一职能原则
            if (vip.equals(2)) {
                money = money * 0.75;
                corn = 29900;
            } else if (vip.equals(3)) {
                money = money * 0.5;
                corn = 1000000;
            } else {
                money = money * 1;
                corn = 0;
            }

            // 开始根据价格进行相应的支付逻辑处理
            log.info("购买的课程金额为：{}, 学习币为：{}", money, corn);

            return "success";
        }
        return "fail";
    }

    /**
     * description: 测试
     *
     * @author: zhang hang
     **/
    @Test
    public void testBuyCourse() {
        String s = buyCourse(1L, 1L);
    }

}
